package cmd

import (
	"flag"
	"fmt"
	"os"
)

type StartCommand struct {
	fs *flag.FlagSet

	path string
}

func NewStartCommand() *StartCommand {
	sc := &StartCommand{
		fs: flag.NewFlagSet("start", flag.ExitOnError),
	}
	sc.fs.StringVar(&sc.path, "path", "", "Script path  to start")
	return sc
}

func (s *StartCommand) Name() string {
	return s.fs.Name()
}

func (s *StartCommand) Init(args []string) error {
	return s.fs.Parse(args)
}

func (s *StartCommand) Check() error {
	if s.path == "" {
		return fmt.Errorf("must specify script path with -path")
	}
	return nil
}

func (s *StartCommand) Run() error {
	return StartProcess(s.path)
}

func (s *StartCommand) Usage() {
	fmt.Fprintf(os.Stderr, "Usage: %s start [options]\n", os.Args[0])
	fmt.Fprintln(os.Stderr, "Options:")
	s.fs.PrintDefaults()
	fmt.Fprintln(os.Stderr, "\nExamples:")
	fmt.Fprintln(os.Stderr, "  Start program: cmd-manager start -name myapp")
	fmt.Fprintln(os.Stderr, "  Start script:  cmd-manager start -name \"./start.sh\"")
}
